<?php
/**
 * User: i@fedax.cn
 * Time: 2018/7/27 17:45
 */

namespace app\admin\model;


use OSS\Core\OssException;
use OSS\OssClient;
use Qsnh\think\Upload\Upload;
use think\facade\Config;
use think\File;

class Gift extends DataBase
{
    protected $name = 'gift_url';
    protected $autoWriteTimestamp = true;

    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d H:i:s', $value);
    }
    public function getIsPutawayAttr($value)
    {
        $status = [1 => '否', 0 => '是'];
        return $status[$value];
    }
    public function getTypeImgAttr($value)
    {
        return !empty($value) ? $value : '无';
    }
    public function getGiftAnimationAttr($value)
    {
        return !empty($value) ? $value : '无';
    }

    /**
     * @param $page_config
     * @param $map
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getDataList()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit',10000),
        );

        $where=[];
        if(!empty(input('key')) && !empty(input('value'))){
            $key = input('key');$value= input('value');
            if($key =='type' && $value == '普通'){
                $value=1;
            }elseif($key =='type' && $value == '专属'){
                $value=2;
            }
            $where[] = [$key,'=',$value];
        }
        $order = ['status'=>'ASC','sort'=>'DESC'];
        return $this
            ->field('id,gift_name,gift_url,status,type,type_img,gift_animation,sort,gift_type,created_at as create_time')
            ->where($where)
            ->order($order)
            ->paginate($page_config);
    }

    public function add()
    {
     // halt($_FILES);
        $redis = new \Redis();
        $redis->connect('127.0.0.1','6379');
        $redis->auth(\think\facade\Config::get('custom.dev.redis_secret'));
        $newData['gift_url'] = oosUpload($_FILES['gift_url']);
        $newData['type_img'] = oosUpload($_FILES['type_img']);
        $newData['gift_animation'] = oosUpload($_FILES['gift_animation']);
        $newData['gift_name'] = input('post.gift_name');
        $gift_prize = input('post.gift_prize');
        $gift_type = input('post.gift_type');
        if($gift_type == 0) $newData['gold'] = $gift_prize;
        else $newData['diamonds'] = $gift_prize;
        $newData['type'] = input('post.type');
        $newData['status'] = input('post.status');
        $newData['sort'] = input('post.sort');
        $res = $this->save($newData);
        $redis->del('giftList');
        return $res;
    }
}